cd(uigetdir());
list = dir('*metadata.mat');

if ~isempty(list)
    disp('loading metadata');
    load(list.name);
    oldList = vidList;
else
    disp('no existing metadata detected');
    
end

%% generate videos, check validity, and gather metadata
vidList = dir('*.mov');

figure(1);

for i = 1:numel(vidList)
    %% see if video is valid
    
%     match = 0;
%     for j = 1:numel(oldList)
%         if strmatch(vidList(i).name, oldList(j).name)
%             match = 1;
%             vidList(i).valid = oldList(j).valid;
%             vidList(i).arenaPoly = oldList(j).arenaPoly;
%             vidList(i).leftBaitPoly = oldList(j).leftBaitPoly;
%             vidList(i).rightBaitPoly = oldList(j).rightBaitPoly;
%             
%             break
%         end
%     end
    
 %   if match == 0 %If there's no match...
        %%
        try
            vid = VideoReader(vidList(i).name);
            
        catch
            disp('invalid file');
            vidList(i).valid = 0;
            continue
        end
        %% calculate background
        vidList(i).valid = 1;
        
        im = read(vid,1);
        imshow(im);
        
        title('label outer arena');
        outerArena = roipoly();
        
        title('label LEFT bait');
        leftBait = roipoly();
        
        title('label RIGHT bait');
        rightBait = roipoly();
        
        vidList(i).arenaPoly = outerArena;
        vidList(i).leftBaitPoly = leftBait;
        vidList(i).rightBaitPoly = rightBait;
 %   end
end


save('metadata.mat', 'vidList');